<?php
namespace app\service\Protocol;

/**
 * 现有厂商协议适配器
 * 实现原有的MQTT协议（主题：tc/device/in/out）
 */
class Vendor1Protocol implements ProtocolInterface
{
    /**
     * 协议名称
     */
    private const PROTOCOL_NAME = 'Vendor1 (Original)';

    /**
     * 协议版本
     */
    private const PROTOCOL_VERSION = '1.0';

    /**
     * 下行主题前缀
     */
    private const DOWNLINK_TOPIC_PREFIX = 'tc/device/in';

    /**
     * 上行主题前缀
     */
    private const UPLINK_TOPIC_PREFIX = 'tc/device/out';

    /**
     * 构建指令消息
     * 
     * @param string $deviceId 设备MAC地址
     * @param int $type 指令类型
     * @param array $params 指令参数
     * @return array MQTT消息数组
     */
    public function buildCommand(string $deviceId, int $type, array $params): array
    {
        $topic = $this->getDownlinkTopic($deviceId);
        $commands = $this->buildCommandsByType($type, $params);

        $message = [
            'code' => 200,
            'message' => 'OK',
            'cmds' => $commands
        ];

        return [
            'topic' => $topic,
            'message' => json_encode($message, JSON_UNESCAPED_UNICODE)
        ];
    }

    /**
     * 根据指令类型构建命令数组
     * 
     * @param int $type 指令类型
     * @param array $params 参数
     * @return array 命令数组
     */
    private function buildCommandsByType(int $type, array $params): array
    {
        $commands = [];

        switch ($type) {
            case 1: // 修改热点
                $commands[] = [
                    'cmd_name' => 'hotspot_modify',
                    'cmd_params' => [
                        'hotspot_name' => $params['hotspot_name'] ?? '',
                        'hotspot_password' => $params['hotspot_password'] ?? '',
                        'hotspot_connected_num_max' => $params['hotspot_connected_num_max'] ?? 20,
                        'hotspot_whether_hide' => $params['hotspot_whether_hide'] ?? 0,
                        'hotspot_band' => $params['hotspot_band'] ?? 1,
                    ]
                ];
                break;

            case 2: // 切换网络
                $commands[] = [
                    'cmd_name' => 'network_switch',
                    'cmd_params' => [
                        'sim_id' => $params['sim_id'] ?? 1
                    ]
                ];
                break;

            case 3: // 重启设备
                $commands[] = [
                    'cmd_name' => 'reboot',
                    'cmd_params' => [
                        'delay' => $params['delay'] ?? 0
                    ]
                ];
                break;

            case 4: // 网络限速
                $commands[] = [
                    'cmd_name' => 'network_speed_limit',
                    'cmd_params' => [
                        'type' => $params['type'] ?? 'all',
                        'unit' => $params['unit'] ?? 'MB',
                        'value' => $params['value'] ?? 0
                    ]
                ];
                break;

            case 5: // 恢复出厂设置
                $commands[] = [
                    'cmd_name' => 'factory_reset',
                    'cmd_params' => []
                ];
                break;

            case 6: // 热点显隐
                $commands[] = [
                    'cmd_name' => 'hotspot_visibility',
                    'cmd_params' => [
                        'hidden' => $params['hidden'] ?? 0
                    ]
                ];
                break;

            default:
                break;
        }

        return $commands;
    }

    /**
     * 获取下行主题
     * 
     * @param string $deviceId 设备MAC
     * @return string MQTT主题
     */
    public function getDownlinkTopic(string $deviceId): string
    {
        return self::DOWNLINK_TOPIC_PREFIX . '/' . $deviceId;
    }

    /**
     * 获取上行主题
     * 
     * @param string $deviceId 设备MAC
     * @return string MQTT主题
     */
    public function getUplinkTopic(string $deviceId): string
    {
        return self::UPLINK_TOPIC_PREFIX . '/' . $deviceId;
    }

    /**
     * 获取协议名称
     * 
     * @return string
     */
    public function getName(): string
    {
        return self::PROTOCOL_NAME;
    }

    /**
     * 获取协议版本
     * 
     * @return string
     */
    public function getVersion(): string
    {
        return self::PROTOCOL_VERSION;
    }
}

